﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace QQ2564874169.Core.ORM
{
    public interface IDbSqlQuery : IDbCore
    {
        event EventHandler<SqlBeforeEventArgs> Before;
        event EventHandler<SqlAfterEventArgs> After;
        event EventHandler<SqlException> Error;

        T QueryOne<T>(string sql, object param = null, bool isProc = false, int? timeout = null);
        IEnumerable<T> Query<T>(string sql, object param = null, bool isProc = false, int? timeout = null);
        IMultipleReader Multiple(string sql, object param = null, bool isProc = false, int? timeout = null);
        PageResult<T> Pager<T>(PageSet pageset, string sql, object param = null, bool isProc = false, int? timeout = null);
    }

    public class DbSqlQuery : IDbSqlQuery
    {
        public static event Action<IDbSqlQuery> Created;

        public Hashtable Data { get; private set; }
        public bool InTransaction => _sql.InTransaction;
        public event EventHandler<SqlBeforeEventArgs> Before
        {
            add => _sql.Before += value;
            remove => _sql.Before -= value;
        }
        public event EventHandler<SqlAfterEventArgs> After
        {
            add => _sql.After += value;
            remove => _sql.After -= value;
        }
        public event EventHandler<SqlException> Error
        {
            add => _sql.Error += value;
            remove => _sql.Error -= value;
        }
        public event EventHandler<TransactionFinishEventArgs> TransactionBefore
        {
            add => _sql.TransactionBefore += value;
            remove => _sql.TransactionBefore -= value;
        }
        public event EventHandler<TransactionFinishEventArgs> TransactionAfter
        {
            add => _sql.TransactionAfter += value;
            remove => _sql.TransactionAfter -= value;
        }

        private ISql _sql;

        public DbSqlQuery(ISql sql)
        {
            _sql = sql;
            Data = new Hashtable();
            Created?.Invoke(this);
        }

        public virtual void Dispose()
        {
            Data?.Clear();
        }

        public ITransactionScope Transaction()
        {
            return _sql.Transaction();
        }

        public T QueryOne<T>(string sql, object param = null, bool isProc = false, int? timeout = null)
        {
            return _sql.QueryOne<T>(sql, param, isProc, timeout);
        }

        public IEnumerable<T> Query<T>(string sql, object param = null, bool isProc = false, int? timeout = null)
        {
            return _sql.Query<T>(sql, param, isProc, timeout);
        }

        public IMultipleReader Multiple(string sql, object param = null, bool isProc = false, int? timeout = null)
        {
            return _sql.Multiple(sql, param, isProc, timeout);
        }

        public PageResult<T> Pager<T>(PageSet pageset, string sql, object param = null, bool isProc = false, int? timeout = null)
        {
            return _sql.Pager<T>(pageset, sql, param, isProc, timeout);
        }
    }
}
